//
//  SingleCase.m
//  TEST
//
//  Created by huajun on 2017/6/30.
//  Copyright © 2017年 huajun. All rights reserved.
//

#import "SingleCase.h"
#import "AFNetworking.h"

@implementation SingleCase
static SingleCase *manager=nil;
static AFHTTPSessionManager *afnManager = nil;

+ (SingleCase *)manager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SingleCase alloc] init];
        afnManager = [AFHTTPSessionManager manager];
        afnManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        afnManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    });
    
    return manager;
}



/**
 *  提示信息
 *
 *  @param message 要提示的内容
 */
+ (void)showAlertViewWithMessage:(NSString *)message {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}
//GET请求
- (void)getDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure {
    
    [afnManager GET:@"https://itunes.apple.com/lookup?id=1236427625" parameters:nil progress:^(NSProgress *_Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
        
        NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        //获取AppStore的版本号
        NSDictionary *dict = [[resultDic objectForKey:@"results"]objectAtIndex:0];
        NSString *appUrl = [dict valueForKey:@"trackViewUrl"];
        NSString * versionStr =[dict valueForKey:@"version"];
        NSString *versionStr_int=[versionStr stringByReplacingOccurrencesOfString:@"."withString:@""];
        int version=[versionStr_int intValue];
        
        //获取本地的版本号
        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
        NSString * currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];
        NSString *currentVersion_int=[currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""];
        int current = [currentVersion_int intValue];
        
        if(version > current){
           
            NSLog(@"%d",version );
        }
    } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
        failure(error);
    }];

    
}

//POST请求
- (void)postDataWithUrl:(NSString *)url parameters:(NSDictionary *)paramters success:(Success)success failure:(Failure)failure {
    
    [afnManager POST:url parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            
        }else {
            [[self class] showAlertViewWithMessage:@"暂无数据"];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            [[self class] showAlertViewWithMessage:@"服务器出错了~~~~(>_<)~~~~"];
            failure(error);
        }
    }];
}

//+(SingleCase *)shareInstance
//
//{
//    
//    if (instacne==nil) {
//        
//        instacne=[[SingleCase alloc]init];
//        
//    }
//    
//    return instacne;
//    
//}
//
//
////创建对象alloc时调用
//
//+(id)allocWithZone:(struct _NSZone *)zone
//
//{
//    
//    if (instacne==nil) {
//        
//        instacne=[super allocWithZone:zone];
//        
//    }
//    
//    return instacne;
//    
//    
//}

@end
